﻿using Common.Communal;
using ERP.Domain.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Repository.Interface;
using System.Text;
using System.Threading.Tasks;

namespace System.Services.Users
{
    /// <summary>
    /// 用户服务接口
    /// </summary>
    public class UserService : IUserService
    {
        /// <summary>
        /// 用户仓储接口
        /// </summary>
        private readonly IUserRepository userRepository;
        /// <summary>
        /// 构造函数，注入用户仓储接口
        /// </summary>
        /// <param name="userRepository">用户仓储接口</param>
        public UserService(IUserRepository userRepository)
        {
            this.userRepository = userRepository;
        }
        public async Task<ApiResult<int>> Add(User user)
        {
            var result = new ApiResult<int>();
            try
            {
                if (user == null)
                {
                    result.Error(result.Message = "用户信息不能为空");
                    return result;
                }
                if (string.IsNullOrWhiteSpace(user.UserNo))
                {
                    result.Error(result.Message = "用户编号不能为空");
                    return result;
                }
                if (user.UserNo.Length > 50)
                {
                    result.Error(result.Message = "用户编号长度不能超过50个字符");
                    return result;
                }
                if (string.IsNullOrWhiteSpace(user.UserName))
                {
                    result.Error(result.Message = "用户名称不能为空");
                    return result;
                }
                if (user.UserName.Length > 50)
                {
                    result.Error(result.Message = "用户名称长度不能超过50个字符");
                    return result;
                }
                if (string.IsNullOrWhiteSpace(user.UserPassword))
                {
                    result.Error(result.Message = "用户密码不能为空");
                    return result;
                }
                if (user.UserPassword.Length > 100)
                {
                    result.Error(result.Message = "用户密码长度不能超过100个字符");
                    return result;
                }
                var account=await userRepository.GetAsync(u => u.UserAccount == user.UserAccount && !u.IsDeleted);
                if(account != null)
                {
                    result.Error(result.Message = "用户账户已存在");
                    return result;
                }
                var list =await userRepository.GetAsync(u => u.UserNo == user.UserNo && !u.IsDeleted);
                if (list.Count()>0)
                {
                    result.Error(result.Message = "用户编号已存在");
                    return result;
                }
                user.UserStatus = true;
                user.IsDeleted = false;
                var res = await userRepository.AddAsync(user);
                if (res > 0)
                {
                    result.Success(res, result.Message = "用户添加成功");
                }
                else
                {
                    result.Error(result.Message = "用户添加失败");
                }
            }
            catch (Exception ex)
            {
                result.Error(result.Message = $"用户添加异常: {ex.Message}");
            }
            return result;
        }
        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="user">实体</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult<int>> Update(User user)
        {
            var result = new ApiResult<int>();
            try
            {
                if (user.UserNo.Length > 50)
                {
                    result.Error(result.Message = "用户编号长度不能超过50个字符");
                    return result;
                }
                if (user.UserAccount.Length > 50)
                {
                    result.Error(result.Message = "用户账户长度不能超过50个字符");
                    return result;
                }
                if (user.UserName.Length > 50)
                {
                    result.Error(result.Message = "用户名称长度不能超过50个字符");
                    return result;
                }
                if (user.UserPassword.Length > 255)
                {
                    result.Error(result.Message = "用户密码长度不能超过255个字符");
                    return result;
                }
                if (!string.IsNullOrWhiteSpace(user.UserPhone) && user.UserPhone.Length > 11)
                {
                    result.Error(result.Message = "手机号长度不能超过11个字符");
                    return result;
                }
                if (!string.IsNullOrWhiteSpace(user.UserEmail) && user.UserEmail.Length > 100)
                {
                    result.Error(result.Message = "电子邮箱长度不能超过100个字符");
                    return result;
                }
                if (!string.IsNullOrWhiteSpace(user.OrganzationNo) && user.OrganzationNo.Length > 50)
                {
                    result.Error(result.Message = "组织机构编号长度不能超过50个字符");
                    return result;
                }
                if (!string.IsNullOrWhiteSpace(user.PostNo) && user.PostNo.Length > 50)
                {
                    result.Error(result.Message = "岗位编号长度不能超过50个字符");
                    return result;
                }

                // 检查用户账户是否与其他用户重复
                var res = await userRepository.GetAsync(u => u.UserAccount == user.UserAccount && u.Id != user.Id && !u.IsDeleted);
                if (res.Count()>0)
                {
                    result.Error(result.Message = "用户账户已存在");
                    return result;
                }

                var list = await userRepository.Update(user);
                if (list > 0)
                {
                    result.Success(list, result.Message = "用户更新成功");
                }
                else
                {
                    result.Error(result.Message = "用户更新失败");
                }
            }
            catch (Exception ex)
            {
                result.Error(result.Message = $"用户更新异常: {ex.Message}");
            }
            return result;
        }

        /// <summary>
        /// 启用/禁用用户
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult<int>> Enable(int id)
        {
            var result = new ApiResult<int>();
            try
            {
                var user = await userRepository.GetModelAsync(id);
                user.UserStatus = !user.UserStatus;
                var res = await userRepository.Update(user);
                if (res > 0)
                {
                    string statusText = user.UserStatus ? "启用" : "禁用";
                    result.Success(res, result.Message = $"用户{statusText}成功");
                }
                else
                {
                    result.Error(result.Message = "用户状态更新失败");
                }
            }
            catch (Exception ex)
            {
                result.Error(result.Message = $"用户状态更新异常: {ex.Message}");
            }
            return result;
        }

        /// <summary>
        /// 逻辑删除用户
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult<int>> Delete(int id)
        {
            var result = new ApiResult<int>();
            try
            {
                var user = await userRepository.GetModelAsync(id);
                // 执行逻辑删除
                user.IsDeleted = true;
                var res = await userRepository.Update(user);
                if (res > 0)
                {
                    result.Success(res, result.Message = "用户删除成功");
                }
                else
                {
                    result.Error(result.Message = "用户删除失败");
                }
            }
            catch (Exception ex)
            {
                result.Error(result.Message = $"用户删除异常: {ex.Message}");
            }
            return result;
        }
    }
}
